package com.jyy.prefabricated.controller;


import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.entity.CertificateCategory;
import com.jyy.prefabricated.service.CertificateCategoryService;
import com.jyy.prefabricated.service.CertificateCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 证书种类
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class CertificateCategoryController {

    @Autowired
    private CertificateCategoryService categoryService;

    /**
     * 新增证书种类
     * @param certificateCategory
     * @return
     */
    @PostMapping("/certificate-category")
    public AjaxResult insert(@Valid @RequestBody CertificateCategory certificateCategory){
        log.info("Rest request to insert certificateCategory:{}",certificateCategory);
        Integer id = categoryService.insert(certificateCategory);
        CertificateCategory result = categoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新证书种类
     * @param certificateCategory
     * @return
     */
    @PutMapping("/certificate-category")
    public AjaxResult update(@Valid @RequestBody CertificateCategory certificateCategory){
        log.info("Rest request to update certificateCategory:{}",certificateCategory);
        categoryService.update(certificateCategory);
        CertificateCategory result = categoryService.selectById(certificateCategory.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询证书种类详情
     * @param id
     * @return
     */
    @GetMapping("/certificate-category/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find certificateCategory by id:{}",id);
        CertificateCategory result = categoryService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除证书种类
     * @param id
     * @return
     */
    @DeleteMapping("/certificate-category/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete certificateCategory by id:{}",id);
        int row = categoryService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询证书种类列表
     * @return
     */
    @GetMapping("/certificate-category")
    public AjaxResult selectAll(){
        log.info("Rest request to find certificateCategory list");
        List<CertificateCategory> result = categoryService.selectAll();
        return AjaxResult.success(result);
    }
}
